package com.android.support.lib.universal.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.android.support.lib.universal.R;
import com.android.support.lib.universal.base.BaseActivity;

/**
 * 启动页,子类只需调用setSplash(int resId, Class mainActivity)即可。
 * 子类不用添加布局文件。
 *
 * @author 彭治铭
 */
public class StartActivity extends BaseActivity {

    ImageView Splash;//启动界面图片
    Class mainActivity;//跳转的主Activity

    /**
     * @param resId        启动界面加载的资源图片
     * @param mainActivity 所要跳转的主Activity，如：MainActivity.class
     */
    protected void setSplash(int resId, Class mainActivity) {
        Splash.setImageResource(resId);
        this.mainActivity = mainActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initUI();
        listenner();
    }

    @Override
    protected void initUI() {
        // TODO Auto-generated method stub
        setContentView(R.layout.lib_activity_start);
        Splash = (ImageView) findViewById(R.id.imageView);
    }

    @Override
    protected void listenner() {
        // TODO Auto-generated method stub
        //定时器。定时自动销毁当前Activity,并跳转到主Activity
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                UiHelper.toMain(StartActivity.this, mainActivity, true);
            }
        };
        handler.removeMessages(0);
        handler.sendEmptyMessageDelayed(0, 1200);
    }

    @Override
    protected void onDestroy() {
        //资源回收,当ImageView里面没有图片且没有被引用时，随Activity一起销毁。
        Splash.setImageBitmap(null);
        Splash = null;
        super.onDestroy();
    }
}
